package com.auxgroup.hr.front.site.co.process;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.*;
import com.auxgroup.hr.front.dao.repository.PsCWfModHdrRepository;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.modperson.*;
import com.auxgroup.hr.front.service.process.ProcessService;
import com.auxgroup.hr.front.service.process.WfFileService;
import com.auxgroup.hr.front.site.co.BaseCO;
import com.auxgroup.hr.front.site.co.BaseWfCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.modperson.PersonProcessRespVO;
import com.auxgroup.hr.front.ws.ProcessApplyQueDTO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 个人信息修改CO
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/modPers")
public class WfModPersCO extends BaseWfCO {

	private static final Logger logger = LoggerFactory.getLogger(WfModPersCO.class);

	@Autowired
	private ModPersService modPersService;
	
	@Autowired
	private ModEmpPhotoService modEmpPhotoService;
	
	@Autowired
	private ModBankacctService modBankacctService;
	
	@Autowired
	private ModEducatService modEducatService;
	
	@Autowired
	private ModEmpLangService modEmpLangService;
	
	@Autowired
	private ModFamilyService modFamilyService;
	
	@Autowired
	private ModItTechService modItTechService;
	
	@Autowired
	private ModJcxxService modJcxxService;
	
	@Autowired
	private ModSocialService modSocialService;
	
	@Autowired
	private ModTactPerService modTactPerService;
	
	@Autowired
	private ModTrainService modTrainService;
	
	@Autowired
	private ModWorkHiService modWorkHiService;
	
	@Autowired
	private ModZcglService modZcglService;

	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;
	
	@Autowired
	private PsCWfModHdrRepository psCWfModHdrRepository;



	/**
	 * 流程页面的个人信息展示
	 * @param cTransactionNbr
	 * @param user
	 * @return
	 */
	@GetMapping(value="/getProcessPerson")
	public BaseResponseVO getProcessPerson(String cTransactionNbr,@SessionGain("user") UserDTO user){
		//个人信息primary
		if(StringUtils.isBlank(cTransactionNbr)){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"流程编号不能为空");
		}
		PsCModPersData per = modPersService.getModPers(cTransactionNbr);
		if(per==null){
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
		}
		PsCWfModHdr modHdr = psCWfModHdrRepository.findOne(cTransactionNbr);//获取个人信息内容
		if(modHdr==null){
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
		}
		PersonProcessRespVO respVO = objectMapper.map(modHdr, PersonProcessRespVO.class);
		respVO.setPereditTiles(buildDictFieldFromObject(per,false));//先处理个人信息
		buildPersonEditTitle(respVO,cTransactionNbr,modHdr.getEmplid());
		fillBaseWfInfo(user, modHdr, respVO);
		return buildRespone(respVO);
		
	}
	
	private void buildPersonEditTitle(PersonProcessRespVO respVO,String cTransactionNbr,String emplid){
		//照片信息
		PsCModEmpPhoto empPhoto = modEmpPhotoService.getModEmpPhoto(cTransactionNbr);
		if(empPhoto!=null){
			Map<String, Object> photo = new HashMap<String, Object>();
			photo.put("cModStauts", empPhoto.getcModStatus());
			photo.put("imgUrl", modEmpPhotoService.buildEmplPhoto(empPhoto));
			respVO.setEmplPhoto(photo);
		}
		
		//银行信息
		PsCModBankacct bankacct = modBankacctService.getModBankacct(cTransactionNbr);
		if(bankacct!=null){
			respVO.setEditBank(buildDictFieldFromObject(bankacct));
		}
		//教育信息
		List<PsCModEducat> educats = modEducatService.getAllModEducat(cTransactionNbr, emplid);
		if(educats!=null&&!educats.isEmpty()){
			respVO.setEditEdus(buildDictField(educats));
		}else{
			respVO.setEditEdus(new ArrayList<Map<String,Object>>());
		}
		//语言信息
		List<PsCModEmpLang> emplangs = modEmpLangService.getAll(cTransactionNbr,emplid);
		if(emplangs!=null&&!emplangs.isEmpty()){
			respVO.setEditEmpLangs(buildDictField(emplangs));
		}else{
			respVO.setEditEmpLangs(new ArrayList<Map<String,Object>>());
		}
		//家庭信息
		List<PsCModFamily> familys = modFamilyService.getAll(cTransactionNbr,emplid);
		if(familys!=null&&!familys.isEmpty()){
			respVO.setEditFamily(buildDictField(familys));
		}else{
			respVO.setEditFamily(new ArrayList<Map<String,Object>>());
		}
		//计算机能力信息
		List<PsCModItTech> itTechs = modItTechService.getAll(cTransactionNbr,emplid);
		if(itTechs!=null&&!itTechs.isEmpty()){
			respVO.setEditItTech(buildDictField(itTechs));
		}else{
			respVO.setEditItTech(new ArrayList<Map<String,Object>>());
		}
		//奖惩情况信息
		List<PsCModJcxx> jcxxs = modJcxxService.getAll(cTransactionNbr,emplid);
		if(jcxxs!=null&&!jcxxs.isEmpty()){
			respVO.setEditJcxx(buildDictField(jcxxs));
		}else{
			respVO.setEditJcxx(new ArrayList<Map<String,Object>>());
		}
		//紧急联系人信息
		PsCModPhotoTactPerPrimary tactPerPrimary = new PsCModPhotoTactPerPrimary();
		tactPerPrimary.setcTransactionNbr(cTransactionNbr);
		tactPerPrimary.setEmplid(emplid);
		PsCModTactPer tactper = modTactPerService.getModTactPer(tactPerPrimary);
		if(tactper!=null){
			respVO.setEditTactPer(buildDictFieldFromObject(tactper));
		}
		//社保公积金
		List<PsCModSocial> socials = modSocialService.getAll(cTransactionNbr, emplid);
		if(socials!=null&&!socials.isEmpty()){
			respVO.setEditSocial(buildDictField(socials));
		}else{
			respVO.setEditSocial(new ArrayList<Map<String,Object>>());
		}
		//培训经历信息
		List<PsCModTrain> trains = modTrainService.getAll(cTransactionNbr,emplid);
		if(trains!=null&&!trains.isEmpty()){
			respVO.setEditTrain(buildDictField(trains));
		}else{
			respVO.setEditTrain(new ArrayList<Map<String,Object>>());
		}
		//工作历史信息
		List<PsCModWorkHi> workhis = modWorkHiService.getAll(cTransactionNbr,emplid);
		if(workhis!=null&&!workhis.isEmpty()){
			respVO.setEditWorkHi(buildDictField(workhis));
		}else{
			respVO.setEditWorkHi(new ArrayList<Map<String,Object>>());
		}
		//资格证书信息
		List<PsCModZcgl> zcgls = modZcglService.getAll(cTransactionNbr, emplid);
		if(zcgls!=null&&!zcgls.isEmpty()){
			respVO.setEditZcgl(buildDictField(zcgls));
		}else{
			respVO.setEditZcgl(new ArrayList<Map<String,Object>>());
		}
	}

	@Override
	protected Set<String> getAuthFileds() {
		return null;
	}
}
